package org.sdnstproject.interlayer.OF13Protocal;

import java.util.Arrays;
import java.util.Random;

import org.projectfloodlight.openflow.types.U16;
import org.projectfloodlight.openflow.types.U32;
import org.projectfloodlight.openflow.types.U8;
import org.sdnstproject.interlayer.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class FuFlowRemoved {
	private static final Logger log = LoggerFactory.getLogger(FuFlowRemoved.class);
    public byte version = (byte)4;
    public byte type = (byte)11;
    public short length = (short)56;//MINIMUM_LENGTH = 56 TODO
    public long xid = 0x0;
    public long cookie;//U64 TODO
    public int priority;//TODO
    public short reason;//TODO
    public byte tableId;//TableId  TODO
    public long durationSec;//TODO
    public long durationNsec;//TODO
    public int idleTimeout;//TODO
    public int hardTimeout;//TODO
    public long packetCount;//TODO
    public long byteCount;//TODO
    public byte[] match;//TODO
    
	public FuFlowRemoved() {
			
	}
	
    public FuFlowRemoved setField(String field, String value) {
		switch (field) {
		case "version":
			this.version = Byte.parseByte(value);
			break;
		case "type":
			this.type = Byte.parseByte(value);
			break;
		case "length":
			this.length = Short.parseShort(value);
			break;
		case "xid":
			this.xid = Long.parseLong(value);
			break;
		case "cookie":
			this.cookie = Long.parseLong(value);
			break;
		case "priority":
			this.priority = Integer.parseInt(value);
			break;
		case "reason":
			this.reason = Short.parseShort(value);
			break;
		case "tableId":
			this.tableId = Byte.parseByte(value);
			break;
		case "durationSec":
			this.durationSec = Long.parseLong(value);
			break;
		case "durationNsec":
			this.durationNsec = Long.parseLong(value);
			break;
		case "idleTimeout":
			this.idleTimeout = Integer.parseInt(value);
			break;
		case "hardTimeout":
			this.hardTimeout = Integer.parseInt(value);
			break;
		case "packetCount":
			this.packetCount = Long.parseLong(value);
			break;
		case "byteCount":
			this.byteCount = Long.parseLong(value);
			break;
		case "match":
			this.match = new byte[(new Random().nextInt(5)+1)*8+4];//TODO
			new Random().nextBytes(match);
			break;
		}
		return this;
	}
    
    
    public ByteBuf writeTo() {
		ByteBuf bb = Unpooled.directBuffer();
        int startIndex = bb.writerIndex();
        // fixed value property version = 4
        bb.writeByte(version);
        // fixed value property type = 11
        bb.writeByte(type);
        // length is length of variable message, will be updated at the end
        int lengthIndex = bb.writerIndex();
        bb.writeShort(length);

        bb.writeInt(U32.t(xid));
        bb.writeLong(cookie);
        bb.writeShort(U16.t(priority));
        bb.writeByte(U8.t(reason));
        bb.writeByte(tableId);
        bb.writeInt(U32.t(durationSec));
        bb.writeInt(U32.t(durationNsec));
        bb.writeShort(U16.t(idleTimeout));
        bb.writeShort(U16.t(hardTimeout));
        bb.writeLong(packetCount);
        bb.writeLong(byteCount);
        bb.writeBytes(match);

        // update length field
        length = (short)(bb.writerIndex() - startIndex);
        bb.setShort(lengthIndex, length);
        return bb;
    }
    
    @Override
    public String toString() {
        StringBuilder b = new StringBuilder("OFFlowRemovedVer13(");
        b.append("version=").append(version);
        b.append(", ");
        b.append("type=").append(type);
        b.append(", ");
        b.append("length=").append(length);
        b.append(", ");
        b.append("xid=").append(xid);
        b.append(", ");
        b.append("cookie=").append(cookie);
        b.append(", ");
        b.append("priority=").append(priority);
        b.append(", ");
        b.append("reason=").append(reason);
        b.append(", ");
        b.append("tableId=").append(tableId);
        b.append(", ");
        b.append("durationSec=").append(durationSec);
        b.append(", ");
        b.append("durationNsec=").append(durationNsec);
        b.append(", ");
        b.append("idleTimeout=").append(idleTimeout);
        b.append(", ");
        b.append("hardTimeout=").append(hardTimeout);
        b.append(", ");
        b.append("packetCount=").append(packetCount);
        b.append(", ");
        b.append("byteCount=").append(byteCount);
        b.append(", ");
        b.append("match=").append(Arrays.toString(match));
        b.append(")");
        return b.toString();
    }
}
